<?php
require_once APP . '/application/controllers/UserAction.php';
require_once(APP . '/application/models/MeetModel.php');
require_once(APP . '/application/models/Article.php');
require_once(APP . '/application/models/UserModel.php');
require_once(APP . '/application/models/MailogModel.php');

/**
 * 邮件发送记录
 * @package admin
 */
class MaillogController extends UserAction
{
    

    public function init()
    {
        $mid = (int)$this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	
    	if ($mid != 0 && $uid != 0){
    		$exists = $this->db->fetchOne("select count(*) from meet where mid=? and uid=?", array($mid, $uid));
    		if ($exists == false){
    			exit("该会议不是你创建的！");
    		}
    	}
    }
    
    public function searchAction()
    {
    	$mid = (int)$this->getParam("mid");
    	$nowp = $this->getParam ('nowp', 1);
    	$email = $this->getParam("email");
    	
    	$per_page = 30;
    	
    	if (!empty($email)){
	    	$obj = new MailogModel($this->db);
	    	$result = $obj->fetchbyMail($mid, $email, $nowp, $per_page);
	    	 
	    	$arr_strs = array("invite" => '邀请信', "check" => '用户审核', 'confirm' => '最终确认', 'allsend' => '全部群发', "status" => '状态变化');
	    	foreach($result['list'] as $index => $v){
	    		$result['list'][$index]['type'] = $arr_strs[$v['type']];
	    	}
    	}else{
    		$result['list'] = array();
    		$result['page'] = Array(
                                  'totalnum'  => 0,
                                  'totalpage' => 0,
                                  'perpage'   => 0,
                                  'first'     => 0,
                                  'end'       => 0,
                                  'previous'  => 0,
                                  'nowp'      => 0,
                                  'next'      => 0
                                  );
    	}
    	 
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->mid = $mid;
    	$this->view->email = $email;
    	
    	$this->render("search");
    }
    
    
    public function listAction()
    {
    	$mid = (int)$this->getParam("mid");
    	$nowp = $this->getParam ('nowp', 1);
    	$message = $this->getParam("message");
    	$per_page = 30;
    	 
    	$obj = new MailogModel($this->db);
    	$result = $obj->fetchAll ($mid, $message, $nowp, $per_page);
    	
    	$arr_strs = array("invite" => '邀请信', "check" => '用户审核', 'confirm' => '最终确认', 'allsend' => '全部群发', "status" => '状态变化');
    	foreach($result['list'] as $index => $v){
    		$result['list'][$index]['type'] = $arr_strs[$v['type']];
    	}
    	
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->mid = $mid;
    	$this->view->message = $message;
    	 
    	$this->render("list");
    }
    
    public function showAction()
    {
    	$mid = (int)$this->getParam("mid");
    	$id = (int)$this->getParam("id");
    	

    	$obj = new MailogModel($this->db);
    	$result = $obj->show($id, $mid);
    	
    	$this->view->content = $result;
    	$this->render("show");
    }
    
    public function flushAction()
    {
    	$mid = (int)$this->getParam("mid");
    	
    	$this->db->delete("mailog", "mid=" . $mid);
    	
    	$this->showtipmessage("", "清空成功！");
    }
}